<template>
  <div>
    弹框
    <el-tree
      ref="refTree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
import {
  getRoleDetail,
  assignPerm
} from '@/api/setting'
export default {
  name: 'AssignPermission',
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: []
    }
  },
  created() {
    this.loadPermissionList()
    // 调用接口，获取当前这个角色已经具备的权限
    // this.loadPermissionByRoleId()
  },
  methods: {
    async hSubmit() {
      // 1. 组装参数（按接口文档的要求）
      const permIds = this.$refs.refTree.getCheckedKeys() // 当前用户选中的节点的 id
      console.log('permIds', permIds)
      // 2. 调用api
      await assignPerm({ id: this.roleId, permIds: permIds })
      // 3. 关闭弹框
      this.$emit('success')
    },
    async loadPermissionByRoleId() {
      console.log(this.roleId)
      // 根据roleId获取当前这个角色已经具备的权限
      const res = await getRoleDetail(this.roleId)
      //   console.log('获取当前角色的已有的权限点数据', res.data.permIds)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    async loadPermissionList() {
    // 发送请求, 获取权限列表
      const { data } = await getPermissionList()
      //   console.log('权限列表的数据是', data)
      this.permissionData = tranListToTreeData(data)
    }
  }
}
</script>

<style>
</style>
